package cjm.task5.service;

import cjm.task5.mapper.RecordMapper;
import cjm.task5.pojo.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class RecordServiceImpl implements RecordService{
    //升降序标记
    private static Integer orderNum1 = 0;//身体状况
    private static Integer orderNum2 = 0;//是否隔离
    private static Integer orderNum3 = 0;//是否在校
    private static Integer orderNum4 = 0;//体温
    private static Integer orderNum5 = 0;//填写日期
    private static Integer orderNum6 = 0;//id
    private static Integer orderNum7 = 0;//uid
    private static Integer orderNum8 = 0;//姓名
    private static Integer orderNum9 = 0;//手机号
    private static Integer orderNum10 = 0;//紧急联系人
    private static Integer orderNum11 = 0;//紧急联系人电话
    private static Integer orderNum12 = 0;//所在地

    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private AnalysisService analysisService;
    @Autowired
    private RecordService recordService;

    @Override
    public void addRecord(Record record) {
         recordMapper.addRecord(record);
    }

    @Override
    public List<Record> searchOneUserRecord(Integer uid, String keyword) {
        return recordMapper.searchOneUserRecord(uid,keyword);
    }

    @Override
    public List<Record> searchAllRecord(String keyword) {
        return recordMapper.searchAllRecord(keyword);
    }

    @Override
    public int todaySubmit(Integer id) {
        return recordMapper.todaySubmit(id);
    }

    @Override
    public List<String> getAllDateByUid(Integer id) {
        return recordMapper.getAllDateByUid(id);
    }

    @Override
    public Record getRecordByDateAndUid(String date, Integer uid) {
        return recordMapper.getRecordByDateAndUid(date,uid);
    }

    @Override
    public List<Record> findRecordByOneUid(Integer id) {
        return recordMapper.findRecordByOneUid(id);
    }

    @Override
    public List<Record> findRecordAll() {
        return recordMapper.findRecordAll();
    }

    @Override
    public List<Record> overTemperature() {
        return recordMapper.overTemperature();
    }

    @Override
    public List<Record> areaRecord() throws IOException {
        List<String> height = analysisService.getData1();
        List<String> middle = analysisService.getData2();
        List<Record> records = recordService.findRecordAll();
        List<Record> records1 = new ArrayList<>();
        int i;
        int j;
        int k;
        int isIn=0;
        for (i = 0; i < height.size(); i++) {
            for (j = 0; j < records.size(); j++) {
                if (records.get(j).getAddress() != null) {
                    if (height.get(i).indexOf(records.get(j).getAddress()) >= 0) {
                        for(k=0;k<records1.size();k++){
                            if (records1.get(k).getId()==records.get(j).getId()){
                                isIn=1;
                            }
                        }
                        if (isIn==0){
                            records1.add(records.get(j));
                        }
                        isIn=0;
                    }
                }
            }
        }
        for (i = 0; i < middle.size(); i++) {
            for (j = 0; j < records.size(); j++) {
                if (records.get(j).getAddress() != null) {
                    if (middle.get(i).indexOf(records.get(j).getAddress()) >= 0) {
                        for(k=0;k<records1.size();k++){
                            if (records1.get(k).getId()==records.get(j).getId()){
                                isIn=1;
                            }
                        }
                        if (isIn==0){
                            records1.add(records.get(j));
                        }
                        isIn=0;
                    }
                }
            }
        }

        return records1;

    }

    @Override
    public List<Record> RecordOrderBy(Integer id) {
        List<Record> records = new ArrayList<Record>();
        if(id==1){
            records = recordMapper.RecordOrderBy(id,orderNum1);
            if(orderNum1==0){
                orderNum1=1;
            }else {
                orderNum1=0;
            }
        }
        else if(id==2){
            records = recordMapper.RecordOrderBy(id,orderNum2);
            if(orderNum2==0){
                orderNum2=1;
            }else {
                orderNum2=0;
            }
        }
        else if(id==3){
            records = recordMapper.RecordOrderBy(id,orderNum3);
            if(orderNum3==0){
                orderNum3=1;
            }else {
                orderNum3=0;
            }
        }
        else if(id==4){
            records = recordMapper.RecordOrderBy(id,orderNum4);
            if(orderNum4==0){
                orderNum4=1;
            }else {
                orderNum4=0;
            }
        }
        else if(id==5){
            records = recordMapper.RecordOrderBy(id,orderNum5);
            if(orderNum5==0){
                orderNum5=1;
            }else {
                orderNum5=0;
            }
        }
        else if(id==6){
            records = recordMapper.RecordOrderBy(id,orderNum6);
            if(orderNum6==0){
                orderNum6=1;
            }else {
                orderNum6=0;
            }
        }
        else if(id==7){
            records = recordMapper.RecordOrderBy(id,orderNum7);
            if(orderNum7==0){
                orderNum7=1;
            }else {
                orderNum7=0;
            }
        }
        else if(id==8){
            records = recordMapper.RecordOrderBy(id,orderNum8);
            if(orderNum8==0){
                orderNum8=1;
            }else {
                orderNum8=0;
            }
        }
        else if(id==9){
            records = recordMapper.RecordOrderBy(id,orderNum9);
            if(orderNum9==0){
                orderNum9=1;
            }else {
                orderNum9=0;
            }
        }
        else if(id==10){
            records = recordMapper.RecordOrderBy(id,orderNum10);
            if(orderNum10==0){
                orderNum10=1;
            }else {
                orderNum10=0;
            }
        }
        else if(id==11){
            records = recordMapper.RecordOrderBy(id,orderNum11);
            if(orderNum11==0){
                orderNum11=1;
            }else {
                orderNum11=0;
            }
        }
        else if(id==12){
            records = recordMapper.RecordOrderBy(id,orderNum12);
            if(orderNum12==0){
                orderNum12=1;
            }else {
                orderNum12=0;
            }
        }
        return records;
    }

    @Override
    public List<Record> getRecordByKeyAndOrder(String keyword, Integer id) {
        List<Record> records = new ArrayList<Record>();
        if(id==1){
            records = recordMapper.getRecordByKeyAndOrder(keyword,id,orderNum1);
            if(orderNum1==0){
                orderNum1=1;
            }else {
                orderNum1=0;
            }
        }
        else if(id==2){
            records = recordMapper.getRecordByKeyAndOrder(keyword,id,orderNum2);
            if(orderNum2==0){
                orderNum2=1;
            }else {
                orderNum2=0;
            }
        }
        else if(id==3){
            records = recordMapper.getRecordByKeyAndOrder(keyword,id,orderNum3);
            if(orderNum3==0){
                orderNum3=1;
            }else {
                orderNum3=0;
            }
        }
        else if(id==4){
            records = recordMapper.getRecordByKeyAndOrder(keyword,id,orderNum4);
            if(orderNum4==0){
                orderNum4=1;
            }else {
                orderNum4=0;
            }
        }
        else if(id==5){
            records = recordMapper.getRecordByKeyAndOrder(keyword,id,orderNum5);
            if(orderNum5==0){
                orderNum5=1;
            }else {
                orderNum5=0;
            }
        }
        else if(id==6){
            records = recordMapper.getRecordByKeyAndOrder(keyword,id,orderNum6);
            if(orderNum6==0){
                orderNum6=1;
            }else {
                orderNum6=0;
            }
        }
        else if(id==7){
            records = recordMapper.getRecordByKeyAndOrder(keyword,id,orderNum7);
            if(orderNum7==0){
                orderNum7=1;
            }else {
                orderNum7=0;
            }
        }
        else if(id==8){
            records = recordMapper.getRecordByKeyAndOrder(keyword,id,orderNum8);
            if(orderNum8==0){
                orderNum8=1;
            }else {
                orderNum8=0;
            }
        }
        else if(id==9){
            records = recordMapper.getRecordByKeyAndOrder(keyword,id,orderNum9);
            if(orderNum9==0){
                orderNum9=1;
            }else {
                orderNum9=0;
            }
        }
        else if(id==10){
            records = recordMapper.getRecordByKeyAndOrder(keyword,id,orderNum10);
            if(orderNum10==0){
                orderNum10=1;
            }else {
                orderNum10=0;
            }
        }
        else if(id==11){
            records = recordMapper.getRecordByKeyAndOrder(keyword,id,orderNum11);
            if(orderNum11==0){
                orderNum11=1;
            }else {
                orderNum11=0;
            }
        }
        else if(id==12){
            records = recordMapper.getRecordByKeyAndOrder(keyword,id,orderNum12);
            if(orderNum12==0){
                orderNum12=1;
            }else {
                orderNum12=0;
            }
        }
        else {
            records = recordMapper.searchAllRecord(keyword);
        }
        return records;
    }
}
